package com.hqd.ch03.v28.spel.ast;

/**
 * 加法运算符
 */
public class OpPlus extends Operator {
    public OpPlus(int startPos, int endPos, SpelNodeImpl... operands) {
        super("+", startPos, endPos, operands);
    }

    @Override
    protected Object getValueInternal(Object left, Object right) {
        if (left instanceof Number && right instanceof Number) {
            Number leftNumber = (Number) left;
            Number rightNumber = (Number) right;
            if (leftNumber instanceof Double || rightNumber instanceof Double) {
                return leftNumber.doubleValue() + rightNumber.doubleValue();
            } else if (leftNumber instanceof Integer || rightNumber instanceof Integer) {
                return leftNumber.intValue() + rightNumber.intValue();
            }
        }
        if (left instanceof String && right instanceof String) {
            return left + (String) right;
        }
        if (left instanceof String) {
            return left + String.valueOf(right);
        }
        if (right instanceof String) {
            return String.valueOf(left) + right;
        }
        return null;
    }
}
